using CCWin.SkinControl;
using ERPWinFormWork.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERPWinFormWork.Login
{
    public partial class ERPHomePage : Form
    {
        public ERPHomePage()
        {
            InitializeComponent();
            InitializeHomePage();
        }

        private void InitializeHomePage()
        {
            // 设置页面背景
            this.BackColor = Color.FromArgb(240, 245, 250);

            // 创建标题面板
            Panel titlePanel = new Panel();
            titlePanel.Dock = DockStyle.Top;
            titlePanel.Height = 80;
            titlePanel.BackColor = Color.FromArgb(60, 148, 212);
            titlePanel.Padding = new Padding(20, 10, 20, 10);

            // 添加标题
            Label titleLabel = new Label();
            titleLabel.Dock = DockStyle.Fill;
            titleLabel.Text = "企业资源规划系统 (ERP)";
            titleLabel.Font = new Font("微软雅黑", 24, FontStyle.Bold);
            titleLabel.ForeColor = Color.White;
            titleLabel.TextAlign = ContentAlignment.MiddleCenter;
            titlePanel.Controls.Add(titleLabel);

            // 创建功能卡片容器
            FlowLayoutPanel cardPanel = new FlowLayoutPanel();
            cardPanel.Dock = DockStyle.Fill;
            cardPanel.Padding = new Padding(30);
            cardPanel.AutoScroll = true;
            cardPanel.FlowDirection = FlowDirection.LeftToRight;
            cardPanel.WrapContents = true;
            cardPanel.BackColor = this.BackColor;

            // 创建功能卡片
            // 暂时注释掉图标引用
            CreateFeatureCard(cardPanel, "采购销售管理", "管理采购订单、销售报价、客户单价等", null);
            CreateFeatureCard(cardPanel, "产品仓储管理", "管理产品库存、入库出库记录等", null);
            CreateFeatureCard(cardPanel, "物料仓储管理", "管理原材料库存、采购入库等", null);
            CreateFeatureCard(cardPanel, "质检管理", "管理产品和物料的质量检验流程", null);
            CreateFeatureCard(cardPanel, "生产管理", "管理生产计划、工单任务和报工记录", null);

            // 添加信息面板
            Panel infoPanel = new Panel();
            infoPanel.Dock = DockStyle.Bottom;
            infoPanel.Height = 50;
            infoPanel.BackColor = Color.White;
            infoPanel.BorderStyle = BorderStyle.Top;
            infoPanel.Padding = new Padding(20, 10, 20, 10);

            Label infoLabel = new Label();
            infoLabel.Dock = DockStyle.Fill;
            infoLabel.Text = "当前用户: " + (TokenManager.CurrentUser?.UserName ?? "未登录") + " | 登录时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            infoLabel.Font = new Font("微软雅黑", 10);
            infoLabel.ForeColor = Color.Gray;
            infoPanel.Controls.Add(infoLabel);

            // 添加所有控件到窗体
            this.Controls.Add(cardPanel);
            this.Controls.Add(titlePanel);
            this.Controls.Add(infoPanel);
        }

        private void CreateFeatureCard(FlowLayoutPanel parent, string title, string description, Image icon)
        {
            // 创建卡片
            Panel card = new Panel();
            card.Size = new Size(260, 220);
            card.Margin = new Padding(20);
            card.BackColor = Color.White;
            card.BorderStyle = BorderStyle.None;
            card.Cursor = Cursors.Hand;
            card.Click += (sender, e) => Card_Click(title);
            card.MouseEnter += (sender, e) =>
            {
                card.BackColor = Color.FromArgb(245, 248, 250);
                card.BorderStyle = BorderStyle.FixedSingle;
            };
            card.MouseLeave += (sender, e) =>
            {
                card.BackColor = Color.White;
                card.BorderStyle = BorderStyle.None;
            };

            // 添加图标（如果有）
            if (icon != null)
            {
                PictureBox iconBox = new PictureBox();
                iconBox.Size = new Size(64, 64);
                iconBox.Location = new Point((card.Width - 64) / 2, 20);
                iconBox.Image = icon;
                iconBox.SizeMode = PictureBoxSizeMode.StretchImage;
                card.Controls.Add(iconBox);
            }
            else
            {
                // 如果没有图标，调整标题位置
                Label titleLabel = new Label();
                titleLabel.Text = title;
                titleLabel.Font = new Font("微软雅黑", 14, FontStyle.Bold);
                titleLabel.Location = new Point(10, 40);
                titleLabel.Size = new Size(card.Width - 20, 30);
                titleLabel.TextAlign = ContentAlignment.MiddleCenter;
                card.Controls.Add(titleLabel);

                // 添加描述
                Label descLabel = new Label();
                descLabel.Text = description;
                descLabel.Font = new Font("微软雅黑", 10);
                descLabel.Location = new Point(10, 80);
                descLabel.Size = new Size(card.Width - 20, 60);
                descLabel.TextAlign = ContentAlignment.MiddleCenter;
                descLabel.ForeColor = Color.Gray;
                descLabel.Multiline = true;
                card.Controls.Add(descLabel);
                return;
            }

            // 添加标题
            Label titleLabel = new Label();
            titleLabel.Text = title;
            titleLabel.Font = new Font("微软雅黑", 14, FontStyle.Bold);
            titleLabel.Location = new Point(10, 90);
            titleLabel.Size = new Size(card.Width - 20, 30);
            titleLabel.TextAlign = ContentAlignment.MiddleCenter;
            card.Controls.Add(titleLabel);

            // 添加描述
            Label descLabel = new Label();
            descLabel.Text = description;
            descLabel.Font = new Font("微软雅黑", 10);
            descLabel.Location = new Point(10, 120);
            descLabel.Size = new Size(card.Width - 20, 60);
            descLabel.TextAlign = ContentAlignment.MiddleCenter;
            descLabel.ForeColor = Color.Gray;
            descLabel.Multiline = true;
            card.Controls.Add(descLabel);

            // 添加到容器
            parent.Controls.Add(card);
        }

        private void Card_Click(string title)
        {
            // 这里可以添加点击卡片后的导航逻辑
            if (this.ParentForm is Master master)
            {
                switch (title)
                {
                    case "采购销售管理":
                        master.采购销售ToolStripMenuItem.PerformClick();
                        break;
                    case "产品仓储管理":
                        master.产品仓储管理ToolStripMenuItem.PerformClick();
                        break;
                    case "物料仓储管理":
                        master.物料仓储管理ToolStripMenuItem.PerformClick();
                        break;
                    case "质检管理":
                        master.质检管理ToolStripMenuItem.PerformClick();
                        break;
                    case "生产管理":
                        master.生产管理ToolStripMenuItem.PerformClick();
                        break;
                }
            }
        }

        // 设计器生成的组件初始化方法
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // ERPHomePage
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1785, 670);
            this.Name = "ERPHomePage";
            this.Text = "ERP首页";
            this.ResumeLayout(false);
        }
    }
}